package edu.sjb.characters;

import edu.sjb.Drawable;
import edu.sjb.constants.ImageConstants;
import edu.sjb.constants.ScoreConstants;
import edu.sjb.threads.SoundThread;

public class Goomba extends GameCharacter {

	public Goomba() {
		super();
	}

	@Override
	public void hitAgainstMario(Mario m) {
		if (!m.underStarEffect()) {
			if ((m.posY() <= posY() - 16) && !m.underSuperMushroomEffect()) {
				game().changeScore(ScoreConstants.GOOMBA_CRUSH);
				SoundThread.goombaCrush();
				die();
			}
			else {
				if (m.underSuperMushroomEffect()) {
					if ((m.posY() + 32 >= posY())) {
						game().mario().setUnderSuperMushroomEffect(false);
						m.shrink();
						if (m.posX() < posX()) {
							m.shiftPosX(m.posX() + 32);
						}
						else {
							m.setPosX(m.posX() - 64);
						}
					}
					else {
						game().changeScore(ScoreConstants.GOOMBA_CRUSH);
						SoundThread.goombaCrush();
						die();
					}
				}
				else {
					if (!m.gravity().onAir()) {
						game().changeScore(ScoreConstants.GOOMBA_HIT);
						m.die();
					}
				}
			}
		}
		else {
			SoundThread.goombaCrush();
			game().changeScore(ScoreConstants.GOOMBA_CRUSH);
			die();
		}
	}

	@Override
	public void characterCollide(GameCharacter c) {

	}

	@Override
	public void die() {
		image().changeImage(ImageConstants.GOOMBA_CRUSHED);
		try {
			Thread.sleep(20);
		}
		catch (InterruptedException e) {
		}
		game().removeEntity(this);
	}

	@Override
	public void run() {
		while (true) {
			for (int i = 0; i < 150 && canGoRight(); i++) {
				if (!canGoLeft()) {
					setCanGoLeft(true);
				}
				shiftPosX(15);
				try {
					Thread.sleep(40);
				}
				catch (InterruptedException e) {
				}
			}
			for (int i = 0; i < 150 && canGoLeft(); i++) {
				if (!canGoRight()) {
					setCanGoRight(true);
				}
				shiftPosX(-15);
				try {
					Thread.sleep(40);
				}
				catch (InterruptedException e) {
				}
			}
		}
	}

	// consultas

	protected Object clone() {
		Goomba clone = new Goomba();
		Drawable image = new Drawable(ImageConstants.GOOMBA_WALK, false);
		clone.setImage(image);
		return clone;
	}

}
